﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using Wicresoft.Framework.Organization;
using Wicresoft.Framework.Authentication;
using System.Web.Routing;

namespace System.Web.BizCode
{
    public class Consts
    {
        static Consts()
        {
            //PortalID = new Guid(ConfigurationManager.AppSettings["TeacherInfo.PortalID"]);
            //IndexUrl = ConfigurationManager.AppSettings["TeacherInfo.IndexUrl"];
        }
        public static Guid PortalID;
        public static string IndexUrl;

        public static User CurrentUser
        {
            get
            {
                
                if (HttpContext.Current.Session["_TempUser"] == null)
                {
                    HttpContext.Current.Session["_TempUser"] = GetCurrentUser();
                }
                return HttpContext.Current.Session["_TempUser"] as User;
            }
        }
        public static Guid CurrentUserID
        {
            get
            {

                if (HttpContext.Current.Session["_TempUserID"] == null)
                {
                    HttpContext.Current.Session["_TempUserID"] = new Guid(Wicresoft.Framework.Common.Util.GetEncryptedCookieValue("WSAF.UserID"));
                }
                return new Guid(HttpContext.Current.Session["_TempUserID"].ToString());
            }
        }
        private static User GetCurrentUser()
        {
            User currentUser = AuthenticationContext.CurrentUser;
            if (currentUser != null)
            {
                return currentUser;
            }
            string returnUrl = HttpContext.Current.Request.Url.OriginalString;
            if (HttpContext.Current.Request.Url.Segments.Length == 2)
                returnUrl = VirtualPathUtility.AppendTrailingSlash(returnUrl);
            string url = string.Format("{0}?ApplicationID={1}&ReturnUrl={2}", WSAFConfig.ServerSignInUrl, WSAFConfig.ApplicationID, HttpUtility.UrlEncode(returnUrl));
            HttpContext.Current.Response.Redirect(url);
            HttpContext.Current.Response.End();
            return null;
        }
    }
}